﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace cludg.Web.Core.Views.Extensions
{
	public class ViewExtensionRegistry : IViewExtensionRegistry
	{
		protected readonly List<IViewExtension> Extensions = new List<IViewExtension>();
		protected readonly List<IViewExtensionProvider> Providers = new List<IViewExtensionProvider>();

		protected void RegisterProvider(IViewExtensionProvider provider)
		{
			Providers.Add(provider);
			foreach (var ext in provider.GetExtensions())
				Register(ext);
		}

		public ViewExtensionRegistry(IEnumerable<IViewExtensionProvider> providers)
		{
			foreach (var provider in providers)
				RegisterProvider(provider);
		}

		public void Register(IViewExtension extension)
		{
			Extensions.Add(extension);
		}

		public IEnumerable<IViewExtension> Get(string place)
		{
			return Extensions.Where(x => x.Place == place).OrderBy(x => x.Index);
		}
	}
}
